package com.yyy.tcc.service.compensation;

public class ThreadTypeDemo {
    public static void main(String[] args) {
        // main线程是用户线程
        System.out.println("Main线程是否是守护线程: " + Thread.currentThread().isDaemon());  // false

        // 演示用户线程
        Thread userThread = new Thread(() -> {
            try {
                System.out.println("用户线程开始执行");
                Thread.sleep(2000);
                System.out.println("用户线程执行完成");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        userThread.setName("user-thread");
        // 默认就是用户线程
        System.out.println("自定义线程是否是守护线程: " + userThread.isDaemon());  // false

        // 演示守护线程
        Thread daemonThread = new Thread(() -> {
            while (true) {
                try {
                    System.out.println("守护线程执行中...");
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    break;
                }
            }
        });
        daemonThread.setDaemon(true);
        daemonThread.setName("daemon-thread");

        // 启动线程
        userThread.start();
        daemonThread.start();
    }
}
